package cn.lixin.pet.service.impl;

import cn.lixin.basic.service.impl.BaseServiceImpl;
import cn.lixin.basic.utils.CodeGenerateUtils;
import cn.lixin.basic.utils.DistanceUtil;
import cn.lixin.basic.utils.PageList;
import cn.lixin.basic.utils.Point;
import cn.lixin.order.domain.PetAcquisitionOrder;
import cn.lixin.org.domain.Employee;
import cn.lixin.org.domain.Shop;
import cn.lixin.org.mapper.EmployeeMapper;
import cn.lixin.org.mapper.ShopMapper;
import cn.lixin.pet.domain.Pet;
import cn.lixin.pet.domain.SearchMasterMsg;
import cn.lixin.pet.mapper.PetMapper;
import cn.lixin.pet.mapper.SearchMasterMsgMapper;
import cn.lixin.pet.query.SearchMasterMsgQuery;
import cn.lixin.pet.service.ISearchMasterMsgService;
import cn.lixin.user.domain.User;
import cn.lixin.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetServiceImpl petService;

    /**
     * 发布 ---  1查询当前登录的用户
     *          2查询离用户最近的店铺
     *          3保存用户信息（下单、发布都需要用户信息）
     * @param searchMasterMsg
     * @param logininfoId
     */
    @Override
    public void publish(SearchMasterMsg searchMasterMsg, Long logininfoId) {
        // 1查询当前登录的用户（下单、发布都需要用户信息）
        User user = userMapper.loadByLogininfoId(logininfoId); // 根据logininfoId查询user
        // 2查询离用户最近的店铺
        String address = searchMasterMsg.getAddress(); // 卖狗地址
        Point point = DistanceUtil.getPoint(address); // 将地址转换为经纬度
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.loadAll());// 最近的店铺
        // 3保存用户信息（下单、发布都需要用户信息）user_id shop_id 都由对象获取
        searchMasterMsg.setUser(user);
        searchMasterMsg.setShop(nearestShop);
        searchMasterMsgMapper.save(searchMasterMsg);
    }
    /**
     * 未
     * @param searchMasterMsgQuery
     * @param logininfoId
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery searchMasterMsgQuery, Long logininfoId) {
        Employee employee = employeeMapper.loadBylogininfoId(logininfoId); // 查询对应的emp
        Long shopId = employee.getShop_id(); // 查询电源对应的店铺id
        if (shopId != null) { // shopid不为空 说明是店员 可以看店铺对应的信息 为空 说明是员工 可以看所有pet信息
            searchMasterMsgQuery.setShopId(shopId); // 设置shopId
        }
        searchMasterMsgQuery.setState(0); // 未处理
        return super.queryPage(searchMasterMsgQuery); // 高级查询 -- 逻辑在sql
    }

    /**
     * 已处理
     * @param searchMasterMsgQuery
     * @param logininfoId
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery searchMasterMsgQuery, Long logininfoId) {
        Employee employee = employeeMapper.loadBylogininfoId(logininfoId); // 查询对应的emp
        Long shopId = employee.getShop_id(); // 查询电源对应的店铺id
        if (shopId != null) { // shopid不为空 说明是店员 可以看店铺对应的信息
            searchMasterMsgQuery.setShopId(shopId); // 设置shopId
        }
        searchMasterMsgQuery.setState(1); // 已处理
        return super.queryPage(searchMasterMsgQuery); // 高级查询 -- 逻辑在sql
    }

    @Override
    public PageList<SearchMasterMsg> queryUserMsg(SearchMasterMsgQuery searchMasterMsgQuery, Long logininfoId) {
        User user = userMapper.loadByLogininfoId(logininfoId); // 查询对应的user
        searchMasterMsgQuery.setUserId(user.getId()); // 设置query中的userid
        searchMasterMsgMapper.queryData(searchMasterMsgQuery); // 高级查询 -- 逻辑在sql
        return super.queryPage(searchMasterMsgQuery);
    }

    /**
     * 处理未处理信息 --
     *       1.改变状态
     *       2.生成宠物和详情
     *       3.生成订单
     *       4.支付
     *
     * @param pet
     * @param logininfoId
     * @return
     */
    @Override
    public void handle(Pet pet, Long logininfoId) {
        searchMasterMsgMapper.updateStateHandlerById(pet.getSearch_master_msg_id());// 改变状态
        petService.save(pet);// petService的save功能会同时保存宠物和详情
        /*
            生成订单
         */
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(pet.getSearch_master_msg_id());
        Employee employee = employeeMapper.loadBylogininfoId(logininfoId);
        Long employeeId = employee.getId();
        PetAcquisitionOrder petAcquisitionOrder = pet2order(pet, searchMasterMsg, employeeId);// 生成收购订单
        if (petAcquisitionOrder != null){
            System.out.println("收购的金额为："+petAcquisitionOrder.getPrice());
        }

    }

    /**
     * 生成订单的方法
     * @param pet - 宠物
     * @param adopt -寻主消息
     * @param employeeId - 处理的员工id
     * @return
     */
    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt,Long employeeId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(adopt.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(adopt.getUser_id());
        order.setPaytype(0);
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(employeeId);
        return order;
    }
}
